import { message } from 'antd';
import moment from 'moment';
import {
  getPubList,
  getCaresList,
  getInfoOpenList,
  getInfoOpenDetail,
  infoIsTop,
  isHomepage,
  isStatus,
  isLegalDistrict,
  addInfoOpen,
  updateInfoOpen,
  caresDetail,
  deleteCares,
  getArrairOpenDetail,
  deleteInfoOpen,
  trendsDetail,
  trendsAffairDetail,
  updateCares,
  addTrends,
  addMember,
  updateMember,
  deleteMember,
  updateArrairOpen,
  addArrairOpen,
} from './service';

const Model = {
  namespace: 'informationOpen',
  state: {
    infoOpenlData: {},
    caresListData: {},
    trendsListData: {},
    memberListData: {},
    caresDetailData: {},
    trendsDetailData: {},
    TableLegalAffairsRef: {},
    tableRef: {},
    selectedOrgId: undefined, // 选择的组织id
    pubList: [],
  },
  effects: {
    *getCaresList({ payload, resolve }, { call, put, select }) {
      const selectedOrgId = yield select(state => state.informationOpen.selectedOrgId);
      const { organizationId } = yield select(state => state.user.userInfo);
      const params = {
        ...payload,
        orgIdForDataSelect: selectedOrgId || organizationId,
        pageNum: payload.current,
        pageSize: payload.pageSize,
      };
      delete params.current;
      const { createTime } = params;

      if (createTime && createTime.length === 2) {
        params.startTime = moment(createTime[0]).format('YYYY-MM-DD');
        params.endTime = moment(createTime[1]).format('YYYY-MM-DD');
      }

      delete params.createTime;

      const response = yield call(getCaresList, params);

      if (!response.error) {
        const { records, pageNum, total } = response;

        const result = {
          data: records,
          page: pageNum,
          pageSize: payload.pageSize,
          success: true,
          total,
        };

        resolve && resolve(result);

        yield put({
          type: 'save',
          payload: {
            trendsData: result,
          },
        });
      }
    },
    *getPubList({ payload }, { call, put }) {
      const params = {
        ...payload,
      };

      const response = yield call(getPubList, params);
      if (!response.error) {
        yield put({
          type: 'save',
          payload: {
            pubList: response,
          },
        });
      }
    },

    *getInfoOpenList({ payload, resolve }, { call, put, select }) {
      const selectedOrgId = yield select(state => state.informationOpen.selectedOrgId);
      const { organizationId } = yield select(state => state.user.userInfo);
      const params = {
        ...payload,
        orgIdForDataSelect: selectedOrgId || organizationId,
        pageNum: payload.current,
        pageSize: payload.pageSize,
      };
      delete params.current;
      const { searchTime } = params;

      if (searchTime && searchTime.length === 2) {
        params.startTime = moment(searchTime[0]).format('YYYY-MM-DD');
        params.endTime = moment(searchTime[1]).format('YYYY-MM-DD');
      }

      delete params.searchTime;

      const response = yield call(getInfoOpenList, params);

      if (!response.error) {
        const { records, pageNum, total } = response;

        const result = {
          data: records,
          page: pageNum,
          pageSize: payload.pageSize,
          success: true,
          total,
        };
        resolve && resolve(result);

        yield put({
          type: 'save',
          payload: {
            trendsListData: result,
          },
        });
      }
    },
    *getInfoOpenDetail({ payload, resolve }, { call, put }) {
      const response = yield call(getInfoOpenDetail, payload);

      if (!response.error) {
        resolve && resolve(response);
        yield put({
          type: 'save',
          payload: {
            infoOpenlData: response,
          },
        });
      }
    },

    *getArrairOpenDetail({ payload, resolve }, { call, put }) {
      const response = yield call(getArrairOpenDetail, payload);

      if (!response.error) {
        resolve && resolve(response);
        yield put({
          type: 'save',
          payload: {
            infoOpenlData: response,
          },
        });
      }
    },
    *infoIsTop({ payload, resolve }, { call, put }) {
      const response = yield call(infoIsTop, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('操作成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *isHomepage({ payload, resolve }, { call, put }) {
      const response = yield call(isHomepage, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('操作成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },

    *isStatus({ payload, resolve }, { call, put }) {
      const response = yield call(isStatus, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('操作成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },

    *isLegalDistrict({ payload, resolve }, { call, put }) {
      const response = yield call(isLegalDistrict, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('操作成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },

    *getCaresDetail({ payload, resolve }, { call, put }) {
      const response = yield call(caresDetail, payload);

      if (!response.error) {
        resolve && resolve(response);
        yield put({
          type: 'save',
          payload: {
            caresDetailData: response,
          },
        });
      }
    },
    *updateCares({ payload, resolve }, { call, put }) {
      const response = yield call(updateCares, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('关工组织修改成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *deleteCares({ payload }, { call, put }) {
      const response = yield call(deleteCares, payload);

      if (!response.error) {
        message.success('删除成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *deleteInfoOpen({ payload }, { call, put }) {
      const response = yield call(deleteInfoOpen, payload);

      if (!response.error) {
        message.success('删除成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },

    *getTrendsDetail({ payload, resolve }, { call, put }) {
      const response = yield call(trendsDetail, payload);

      if (!response.error) {
        resolve && resolve(response);
        yield put({
          type: 'save',
          payload: {
            trendsDetailData: response,
          },
        });
      }
    },

    *getTrendsAffairDetail({ payload, resolve }, { call, put }) {
      const response = yield call(trendsAffairDetail, payload);

      if (!response.error) {
        resolve && resolve(response);
        yield put({
          type: 'save',
          payload: {
            trendsDetailData: response,
          },
        });
      }
    },

    *selectOrgChange({ payload }, { put }) {
      yield put({
        type: 'save',
        payload: {
          selectedOrgId: payload,
        },
      });

      yield put({
        type: 'tableReload',
      });
    },

    *addInfoOpen({ payload, resolve }, { call, put }) {
      const response = yield call(addInfoOpen, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('发布成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *updateInfoOpen({ payload, resolve }, { call, put }) {
      const response = yield call(updateInfoOpen, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('修改成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },

    *addArrairOpen({ payload, resolve }, { call, put }) {
      const response = yield call(addArrairOpen, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('发布成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *updateArrairOpen({ payload, resolve }, { call, put }) {
      const response = yield call(updateArrairOpen, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('修改成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },

    *addTrends({ payload, resolve }, { call, put }) {
      const response = yield call(addTrends, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('关工动态发布成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *addMember({ payload, resolve }, { call, put }) {
      const response = yield call(addMember, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('成员新增成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *updateMember({ payload, resolve }, { call, put }) {
      const response = yield call(updateMember, payload);

      if (!response.error) {
        resolve && resolve(response);
        message.success('成员信息修改成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
    *deleteMember({ payload }, { call, put }) {
      const response = yield call(deleteMember, payload);

      if (!response.error) {
        message.success('删除成功！');
        yield put({
          type: 'tableReload',
        });
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
    tableReload(state) {
      const tableRef = state.tableRef || {};
      const TableLegalAffairsRef = state.TableLegalAffairsRef || {};
      setTimeout(() => {
        tableRef.current && tableRef.current.reloadAndRest();
        TableLegalAffairsRef.current && TableLegalAffairsRef.current.reloadAndRest();
      }, 0);
      return { ...state };
    },
  },
};
export default Model;
